package com.cloudsigns.factorymanager2.engineering.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.IView.IPatrolCompletionForManagerView;
import com.cloudsigns.factorymanager2.engineering.IView.IpatrolCompletionOrderSureView;
import com.cloudsigns.factorymanager2.engineering.bean.MediaBean;
import com.cloudsigns.factorymanager2.engineering.bean.OrderParentBean;
import com.cloudsigns.factorymanager2.engineering.bean.PatrolOrderBean;
import com.cloudsigns.factorymanager2.engineering.presenter.PatrolCompletionForManagePresenter;
import com.cloudsigns.factorymanager2.engineering.presenter.PatrolCompletionOrderSurePresenter;
import com.cloudsigns.factorymanager2.engineering.presenter.PatrolOrderDetailPresenter;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;
import com.cloudsigns.factorymanager2.project.utils.DateUtil;

/**
 * Created by tony on 2018/6/18.
 */

public class PatrolCompletionOrderSubmitForManagerActivity extends Activity implements View.OnClickListener, IpatrolCompletionOrderSureView {
    private TextView tv_title;
    private ViewGroup rl_back;
    private View rl_region_name;
    private TextView edt_region_name, edt_media_number, edt_patrol_cycle, edt_patrol_company, edt_patrol_responsible;
    private View rl_handle_account, rl_handle_date;
    private TextView edt_handle_account, ed_handle_date;
    private TextView btn_confirm_evaluation;
    private final String TAG = PatrolCompletionOrderSubmitForManagerActivity.class.getSimpleName();
    private MediaBean mediaBean;
    private PatrolOrderBean patrolOrderBean;
    private int mediaSize;
    private boolean isClick;
    private PatrolCompletionOrderSurePresenter patrolCompletionOrderSurePresenter;
    private String assignReceiveOrderId, orderId;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        // 沉浸式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    );
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN

                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);

        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        setContentView(R.layout.layout_partrol_completion_order_for_super);

        patrolCompletionOrderSurePresenter = new PatrolCompletionOrderSurePresenter(this);
        init();

        Intent mIntent = getIntent();
        if (null != mIntent) {
            mediaBean = (MediaBean) mIntent.getSerializableExtra(ConstantUtil.KEY_2);
            patrolOrderBean = (PatrolOrderBean) mIntent.getSerializableExtra("PatrolOrderBean");
            mediaSize = mIntent.getIntExtra("mediaSize", 0);

            if (null != patrolOrderBean && null != mediaBean) {
                orderId = patrolOrderBean.getId();
                assignReceiveOrderId = mediaBean.getAssignReceiveOrderId();
            }

        }

        initData();
    }

    private void init() {
        tv_title = findViewById(R.id.tv_title);
        rl_back = findViewById(R.id.rl_back);
        rl_region_name = findViewById(R.id.rl_region_name);
        edt_region_name = findViewById(R.id.edt_region_name);
        edt_media_number = findViewById(R.id.edt_media_number);
        edt_patrol_cycle = findViewById(R.id.edt_patrol_cycle);
        edt_patrol_company = findViewById(R.id.edt_patrol_company);
        edt_patrol_responsible = findViewById(R.id.edt_patrol_responsible);
        edt_handle_account = findViewById(R.id.edt_handle_account);
        ed_handle_date = findViewById(R.id.ed_handle_date);
        rl_handle_account = findViewById(R.id.rl_handle_date);
        rl_handle_date = findViewById(R.id.rl_handle_date);
        btn_confirm_evaluation = findViewById(R.id.btn_confirm_evaluation);


        rl_region_name.setVisibility(View.GONE);

        tv_title.setText("巡查");
        rl_back.setOnClickListener(this);
        btn_confirm_evaluation.setOnClickListener(this);
    }

    private void initData() {

        if (null != patrolOrderBean) {
            edt_patrol_cycle.setText(DateUtil.getYyyy_Mm_Dd(patrolOrderBean.getBeginDate()) + "/" + DateUtil.getYyyy_Mm_Dd(patrolOrderBean.getCompleteDate()));
        }

        if (null != mediaBean) {
//            edt_region_name.setText(mediaBean.getRegionName());
            edt_media_number.setText(mediaSize + "");
            edt_patrol_responsible.setText(mediaBean.getAssignReceiveAccountId() + ":" + mediaBean.getAssignReceiveAccountName());

            CommonEnum.CompleteStatus assignCompleteStatus = mediaBean.getAssignCompleteStatus();
            if (null != assignCompleteStatus) {
                if (assignCompleteStatus == CommonEnum.CompleteStatus.WORKING) {
                    btnClicable(false);
                } else if (assignCompleteStatus == CommonEnum.CompleteStatus.WORKER_COMPLETE) {
                    btnClicable(true);
                } else if (assignCompleteStatus == CommonEnum.CompleteStatus.ADMIN_COMPLETE) {
                    btnClicable(false);
                } else if (assignCompleteStatus == CommonEnum.CompleteStatus.SUPERADMIN_COMPLETE) {
                    btnClicable(false);
                }
            }

//            edt_handle_account.setText(mediaBean.getAssignCompleteAccountId());
//            ed_handle_date.setText(DateUtil.getYyyy_Mm_Dd(mediaBean.getAssignCompleteOrderTime()));
        }
    }

    private void btnClicable(boolean bool) {
        isClick = bool;
        if (bool) {
            btn_confirm_evaluation.setBackgroundResource(R.mipmap.btn_long_bg_green);
            btn_confirm_evaluation.setTextColor(getResources().getColor(R.color.rgb_ffffffff));
        } else {

            btn_confirm_evaluation.setBackgroundResource(R.mipmap.btn_bg_short_gray);
            btn_confirm_evaluation.setTextColor(getResources().getColor(R.color.rgb_ffffffff));
        }
    }

    private void submitCompletion() {
        patrolCompletionOrderSurePresenter.patrolCompletionOrderSure();
    }

    @Override
    public void onClick(View view) {
        Intent mIntent;
        switch (view.getId()) {
            case R.id.rl_back:
                finish();
                break;
            case R.id.btn_confirm_evaluation:
                if (isClick)
                    submitCompletion();
                break;

        }
    }


    @Override
    public String getassignReceiveOrderId() {
        return assignReceiveOrderId;
    }

    @Override
    public String getOderId() {
        return orderId;
    }

    @Override
    public void patrolCompletionOrderSureSuccessed() {
        Toast.makeText(this, "确认成功！", Toast.LENGTH_SHORT).show();
        Intent mIntent = new Intent();
        setResult(RESULT_OK, mIntent);
        finish();
    }

    @Override
    public void patrolCompletionOrderSureFailed(String desc) {
        Toast.makeText(this, desc, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }
}
